package com.whut.monitor.service.impl;

import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.dao.IBaseDao;
import com.whut.monitor.model.CompensatorEntity;
import com.whut.monitor.model.CompensatorEquipmentEntity;
import com.whut.monitor.model.EquipmentEntity;
import com.whut.monitor.service.ICompensatorEquipmentService;
import com.whut.monitor.vo.CompensatorEquipmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author CFlower on 2017/4/5
 * 补偿器设备关联服务层实现
 */

@Service
public class CompensatorEquipmentServiceImpl implements ICompensatorEquipmentService{

    @Autowired
    private IBaseDao<CompensatorEquipmentEntity> compensatorEquipmentDao;

    @Override
    public void add(CompensatorEquipmentVo compensatorEquipmentVo) {
        CompensatorEquipmentEntity entity = new CompensatorEquipmentEntity();

        EquipmentEntity equipt=new EquipmentEntity();
        equipt.setId(compensatorEquipmentVo.getEquipmentId());
        entity.setEquipmentEntity(equipt);

        CompensatorEntity comp=new CompensatorEntity();
        comp.setId(compensatorEquipmentVo.getCompensatorId());
        entity.setCompensatorEntity(comp);

        compensatorEquipmentDao.save(entity);
    }

    @Override
    public void delete(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        compensatorEquipmentDao.executeHql("delete from CompensatorEquipmentEntity entity where entity.id=:id",params);
    }

    @Override
    public void edit(CompensatorEquipmentVo compensatorEquipmentVo) {
        Map<String, Object> params = new HashMap<String, Object>();

        EquipmentEntity equipment=new EquipmentEntity();
        equipment.setId(compensatorEquipmentVo.getEquipmentId());
        equipment.setEquipmentName(compensatorEquipmentVo.getEquipmentName());
        params.put("equipment", equipment);

        CompensatorEntity comp = new CompensatorEntity();
        comp.setId(compensatorEquipmentVo.getCompensatorId());
        comp.setCompensatorName(compensatorEquipmentVo.getCompensatorName());
        params.put("comp", comp);

        params.put("id", compensatorEquipmentVo.getId());
        String hql="update CompensatorEquipmentEntity entity set entity.equipmentEntity=:equipment,entity.compensatorEntity=:comp where entity.id=:id";
        compensatorEquipmentDao.executeHql(hql, params);
    }

    @Override
    public List<CompensatorEquipmentVo> dataGrid(CompensatorEquipmentVo compensatorEquipmentVo, PageFilter pageFilter){
        List<CompensatorEquipmentVo> ul = new ArrayList<CompensatorEquipmentVo>();
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from CompensatorEquipmentEntity entity ";
        List<CompensatorEquipmentEntity> list = compensatorEquipmentDao.findList(hql + whereHql(compensatorEquipmentVo, params) + orderHql(pageFilter), params, pageFilter.getPage(), pageFilter.getRows());
        for (CompensatorEquipmentEntity entity : list) {
            CompensatorEquipmentVo u = new CompensatorEquipmentVo();
            BeanUtils.copyProperties(entity, u);

            u.setEquipmentId(entity.getEquipmentEntity().getId());
            u.setEquipmentName(entity.getEquipmentEntity().getEquipmentName());
            u.setCompensatorId(entity.getCompensatorEntity().getId());
            u.setCompensatorName(entity.getCompensatorEntity().getCompensatorName());
            ul.add(u);
        }
        return ul;
    }

    @Override
    public Long count(CompensatorEquipmentVo compensatorEquipmentVo, PageFilter pageFilter) {
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from CompensatorEquipmentEntity entity ";
        return compensatorEquipmentDao.count("select count(*) " + hql + whereHql(compensatorEquipmentVo, params), params);
    }

    private String whereHql(CompensatorEquipmentVo compensatorEquipmentVo, Map<String, Object> params) {
        String hql = "";
        if (compensatorEquipmentVo != null) {
            hql += " where 1=1 ";
//            if (compensatorEquipmentVo.getChannelName() != null) {
//                hql += " and entity.name like :name";
//                params.put("name", "%%" + compensatorEquipmentVo.getChannelName() + "%%");
//            }
        }
        return hql;
    }

    private String orderHql(PageFilter pageFilter) {
        String orderString = "";
        if ((pageFilter.getSort() != null) && (pageFilter.getOrder() != null)) {
            orderString = " order by entity." + pageFilter.getSort() + " " + pageFilter.getOrder();
        }
        return orderString;
    }

    @Override
    public List<Tree> tree() {
        List<CompensatorEquipmentEntity> l = null;
        List<Tree> lt = new ArrayList<Tree>();

        l = compensatorEquipmentDao.findList("select distinct entity from CompensatorEquipmentEntity entity");

        if ((l != null) && (l.size() > 0)) {
            for (CompensatorEquipmentEntity r : l) {
                Tree tree = new Tree();
                tree.setId(r.getId().toString());
//                tree.setText(r.getEquipmentEntity().getEquipmentName());
//                tree.setText(r.getCompensatorEntity().getCompensatorName());
                lt.add(tree);
            }
        }
        return lt;
    }

    @Override
//	@Transactional
    public List<CompensatorEquipmentEntity> getCompeansatorByEquip(Integer equipmentId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("equipmentId", equipmentId);
        String hql="from CompensatorEquipmentEntity c where c.equipmentEntity.id=:equipmentId";
        List<CompensatorEquipmentEntity> list=compensatorEquipmentDao.findList(hql,params);
        return list;
    }

}
